package com.xhh.apis.config;

import com.heima.utils.thread.ThreadLocalUtil;
import com.xhh.model.user.pojos.User;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

/**
 * @author:xhh
 * @email:3231954428@qq.com
 * @desc:
 * @datatime:2024/7/15 22:35
 */
public class DefaultFeignConfiguration {
    @Bean
    public Logger.Level FeignLoggerLever(){
        return Logger.Level.BASIC;
    }

    @Bean
    public RequestInterceptor userInfoRequestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                // 1. 获取登录用户
                User user = ThreadLocalUtil.getUser();
                if(user == null){
                    // 如果为空，直接跳过
                    return;
                }
                // 2. 如果不为空则放入请求头中，传递给下游微服务
                template.header("userId", user.getId().toString());
            }
        };
    }

}
